#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

class Solution 
{
public:
    string reverseStr(string s, int k) 
    {
        for(int i = 0; i < s.length(); i += 2 * k)
        {
            if(i + k < s.length())
            {
                reverse(s.begin() + i, s.begin() + i + k);
                //String的迭代器可以随机访问，但list这类就不行。
            }
            else
            {
                reverse(s.begin() + i, s.end());
            }
        }
        return s;
    }
};

int main()
{
    Solution sol;

    // 测试用例1
    string s1 = "abcdefg";
    int k1 = 2;
    string res1 = sol.reverseStr(s1, k1);
    cout << "输入: \"abcdefg\", k = 2" << endl;
    cout << "输出: " << res1 << endl; // 期望输出: "bacdfeg"

    // 测试用例2
    string s2 = "abcd";
    int k2 = 2;
    string res2 = sol.reverseStr(s2, k2);
    cout << "输入: \"abcd\", k = 2" << endl;
    cout << "输出: " << res2 << endl; // 期望输出: "bacd"

    // 测试用例3
    string s3 = "a";
    int k3 = 2;
    string res3 = sol.reverseStr(s3, k3);
    cout << "输入: \"a\", k = 2" << endl;
    cout << "输出: " << res3 << endl; // 期望输出: "a"

    // 测试用例4
    string s4 = "";
    int k4 = 2;
    string res4 = sol.reverseStr(s4, k4);
    cout << "输入: \"\", k = 2" << endl;
    cout << "输出: " << res4 << endl; // 期望输出: ""

    // 测试用例5
    string s5 = "abcdefgh";
    int k5 = 3;
    string res5 = sol.reverseStr(s5, k5);
    cout << "输入: \"abcdefgh\", k = 3" << endl;
    cout << "输出: " << res5 << endl; // 期望输出: "cbadefhg"

    return 0;
}
